package ma.xin.shi.configurations.SomeipAction;

import java.util.ArrayList;
import java.util.List;

import ma.xin.shi.AutorsarCommonUtils;
//import ma.xin.shi.configurations.Service.ServiceEntry;

public class SomeipActionConfig {
    private String unicastPort;
    private String unicastProtocol;

    private List<SomeipActionEntry> activeEntries = new ArrayList<>();
    private List<MulticastEntry> multicastEntries = new ArrayList<>();

    // ===== Getters & Setters =====

    public String getUnicastPort() { return unicastPort; }
    public void setUnicastPort(String unicastPort) { this.unicastPort = unicastPort; }

    public String getUnicastProtocol() { return unicastProtocol; }
    public void setUnicastProtocol(String unicastProtocol) { this.unicastProtocol = unicastProtocol; }

    public List<SomeipActionEntry> getActiveEntries() { return activeEntries; }
    public void setActiveEntries(List<SomeipActionEntry> entries) {
        this.activeEntries = entries != null ? entries : new ArrayList<>();
    }

    public List<MulticastEntry> getMulticastEntries() { return multicastEntries; }
    public void setMulticastEntries(List<MulticastEntry> entries) {
        this.multicastEntries = entries != null ? entries : new ArrayList<>();
    }

    // ===== 常用方法 =====

    public void addActiveEntry(SomeipActionEntry entry) {
        if (entry != null) activeEntries.add(entry);
    }

    public boolean removeActiveEntry(String serviceName) {
        return activeEntries.removeIf(e -> e.getServiceName().equals(serviceName));
    }

    public void addMulticastEntry(MulticastEntry entry) {
        if (entry != null) multicastEntries.add(entry);
    }

    public boolean removeMulticastEntry(String address) {
        return multicastEntries.removeIf(e -> e.getSdMulticastAddress().equals(address));
    }

    // 可用于调试或导出时打印信息
    public void printAllConfig() {
        AutorsarCommonUtils.logEx("Unicast Port: " + unicastPort);
        AutorsarCommonUtils.logEx("Unicast Protocol: " + unicastProtocol);

        AutorsarCommonUtils.logEx("\nActive Entries:");
        for (int i = 0; i < activeEntries.size(); i++) {
            SomeipActionEntry entry = activeEntries.get(i);
            System.out.printf("[%d] Service Name: %s, ID: %s%n", i, entry.getServiceName(), entry.getServiceId());
        }

        AutorsarCommonUtils.logEx("\nMulticast Entries:");
        for (int i = 0; i < multicastEntries.size(); i++) {
            MulticastEntry entry = multicastEntries.get(i);
            System.out.printf("[%d] Address: %s, Port: %s%n", i, entry.getSdMulticastAddress(), entry.getSdPortNumber());
        }
    }
    
    public boolean isServiceExist(String serviceName) {
    	boolean existFlg = false;
    	for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
            	existFlg = true;
            	break;
            }
        }
        return existFlg;
    }
    
    public void creatServiceIfNotExist(String serviceName) {
    	if (false == isServiceExist(serviceName)) {
    		SomeipActionEntry entry = new SomeipActionEntry();
    		entry.setServiceName(serviceName);
    		addActiveEntry(entry);
    	}
    }

    public String getServerInstanceIdFromServiceName(String serviceName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getInstanceId();
            }
        }
        return null; // 没有找到返回 null
    }
    
    public void setServerInstanceIdFromServiceName(String serviceName, String id) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                entry.setInstanceId(id);
            }
        }
    }
    
    public String getServerFromServiceName(String serviceName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getServer();
            }
        }
        return null; // 没有找到返回 null
    }
    
    public void setServerFromServiceName(String serviceName, String srver) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                entry.setServer(srver);
            }
        }
    }
    
    public String getClientFromServiceName(String serviceName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getClient();
            }
        }
        return null; // 没有找到返回 null
    }

    public void setClientFromServiceName(String serviceName, String client) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                entry.setClient(client);
            }
        }
    }
    
    public String getClientIDFromServiceName(String serviceName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getClientId();
            }
        }
        return null; // 没有找到返回 null
    }

    public void setClientIDFromServiceName(String serviceName, String clientId) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                entry.setClientId(clientId);
                break;
            }
        }
    }
    
    public String getServiceIDFromServiceName(String serviceName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getServiceId();
            }
        }
        return null; // 没有找到返回 null
    }
    
    public void setServiceIDFromServiceName(String serviceName, String id) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                entry.setServiceId(id);
            }
        }
    }
    
    public String getTTLFromServiceName(String serviceName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getTtl();
            }
        }
        return null; // 没有找到返回 null
    }
    
    public void setTTLFromServiceName(String serviceName, String ttl) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                entry.setTtl(ttl);
            }
        }
    }
    
    public String getVidFromServiceName(String serviceName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getVid();
            }
        }
        return null; // 没有找到返回 null
    }
    
    public void setVidFromServiceName(String serviceName, String vid) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                entry.setVid(vid);
                break;
            }
        }
    }
    
    public String getVidNameFromServiceName(String serviceName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getVName();
            }
        }
        return null; // 没有找到返回 null
    }
    
    public void setVidNameFromServiceName(String serviceName, String vName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                entry.setVName(vName);
                break;
            }
        }
    }
    
    public String getServerPortFromServiceName(String serviceName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getServerPort();
            }
        }
        return null; // 没有找到返回 null
    }
    
    public void setServerPortFromServiceName(String serviceName, String port) {
    	for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                entry.setServerPort(port);
            }
        }
    }
    
    public String getServerVPriorityFromServiceName(String serviceName, String priority) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getVPriority();
            }
        }
        return null; // 没有找到返回 null
    }
    
    public void setServerVPriorityFromServiceName(String serviceName, String priority) {
    	for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                entry.setVPriority(priority);
            }
        }
    }
    
    public String getServerMinverFromServiceName(String serviceName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getMinorVersion();
            }
        }
        return null; // 没有找到返回 null
    }
    
    public void setServerMinverFromServiceName(String serviceName, String ver) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                entry.setMinorVersion(ver);
            }
        }
    }
    //-----------
    public String getServerMajorFromServiceName(String serviceName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getMajorVersion();
            }
        }
        return null; // 没有找到返回 null
    }
    
    public void setServerMajorFromServiceName(String serviceName, String ver) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                entry.setMajorVersion(ver);
            }
        }
    }

    public String getClientPortFromServiceName(String serviceName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getClientPort();
            }
        }
        return null; // 没有找到返回 null
    }
    
    public void setClientPortFromServiceName(String serviceName, String port) {
    	for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                entry.setClientPort(port);
                break;
            }
        }
    }
    
    public String getsetTransportProtocolFromServiceName(String serviceName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getTransportProtocol();
            }
        }
        return null; // 没有找到返回 null
    }
    
    public void setsetTransportProtocolFromServiceName(String serviceName, String protocol) {
    	for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                entry.setTransportProtocol(protocol);
                break;
            }
        }
    }
    
    public String getInstanceIdFromServiceName(String serviceName) {
    	for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry.getInstanceId();
            }
        }
        return null; // 没有找到返回 null
    }
    
    public SomeipActionEntry findFirstMatchingEntry(String serviceName) {
        for (SomeipActionEntry entry : activeEntries) {
            if (serviceName.equals(entry.getServiceName())) {
                return entry;
            }
        }
        return null; // 没有找到返回 null
    }
    
    public boolean isServiceProvidedByTbox(String serviceName) {
        SomeipActionEntry entry = findFirstMatchingEntry(serviceName);
        if (entry != null) {
            return "TBOX".equalsIgnoreCase(entry.getServer());
        }
        return false; // 默认不是提供者
    }
    
    public boolean isServiceProvidedByTboxOfActiveEntry(SomeipActionEntry entry) {
        if (entry != null) {
            return "TBOX".equalsIgnoreCase(entry.getServer());
        }
        return false; // 默认不是提供者
    }
    
    /* 第一条服务名称是 String ecu，比如“TBOX” ， 同时Vlan 是vid比如62 的entry  */
    public SomeipActionEntry findFirstMatchingEntryByVlanAndSECU(int vid, String ecu) {
        for (SomeipActionEntry entry : activeEntries) {
        	AutorsarCommonUtils.logEx("entry.getServer(): " +entry.getServer());
        	
            if (ecu.equals(entry.getServer()) && Integer.toString(vid).equals(entry.getVid())) {
                return entry;
            }
        }
        return null; // 没有找到返回 null
    }

    public boolean isVidExist(String vid) {
    	boolean existFlg = false;
    	for (MulticastEntry entry : multicastEntries) {
            if (vid.equals(entry.getVlanId())) {
            	existFlg = true;
            	break;
            }
        }
        return existFlg;
    }

    /* 检查是否存在特定Vid的条目， 如果不存在则创建一条 */
    public void creatVidIfNotExist(String vid) {
    	if (false == isVidExist(vid)) {
    		MulticastEntry entry = new MulticastEntry();
    		entry.setVlanId(vid);
    		addMulticastEntry(entry);
    	}
    }

    /**
     * 通过Vlan Id获取MulticastEntry 实例
     */
    public MulticastEntry getMulticastEntryByVlan(String vid) {
        for (MulticastEntry entry : multicastEntries) {
            if (entry.getVlanId().equals(vid)) {
                return entry;
            }
        }
        return null;
    }

    public String getMuticasePortWithVid(String vid) {
    	for (MulticastEntry entry : multicastEntries) {
            if (entry.getVlanId().equals(vid)) {
                return entry.getSdPortNumber();
            }
        }
        return null;
    }
    
    public String getMuticaseProtocolWithVid(String vid) {
    	for (MulticastEntry entry : multicastEntries) {
            if (entry.getVlanId().equals(vid)) {
                return entry.getProtocol();
            }
        }
        return null;
    }
    
    public String getMuticaseAddrWithVid(String vid) {
    	for (MulticastEntry entry : multicastEntries) {
            if (entry.getVlanId().equals(vid)) {
                return entry.getSdMulticastAddress();
            }
        }
        return null;
    }
    
    public void setMuticaseAddrWithVid(String vid, String addr) {
    	for (MulticastEntry entry : multicastEntries) {
            if (entry.getVlanId().equals(vid)) {
                entry.setSdMulticastAddress(addr);
            }
        }
    }
    
    /**
     * 在指定条目中 修改 MulticastEntry 的sdPortNumber （通过vlan id）
     */
    public void setMulticastEntryPort(String vlanId, String port) {
    	AutorsarCommonUtils.logEx("in setmulticastEntryPort vlanId: " + vlanId);
    	AutorsarCommonUtils.logEx("in setmulticastEntryPort port: " + port);
    	MulticastEntry entry = getMulticastEntryByVlan(vlanId);
        if (entry != null) {
        	entry.setSdPortNumber(port);
        } else {
        	AutorsarCommonUtils.logEx("in setmulticastEntryPort vlanId is NULL: " +vlanId);
        }
    }
    
    /**
     * 在指定条目中 修改 MulticastEntry 的sdMulticastAddress （通过vlan id）
     */
    public void setMulticastEntryAddr(String vlanId, String addr) {
    	AutorsarCommonUtils.logEx("in setmulticastEntryAddr vlanId: " + vlanId);
    	AutorsarCommonUtils.logEx("in setmulticastEntryAddr addr: " + addr);
    	MulticastEntry entry = getMulticastEntryByVlan(vlanId);
        if (entry != null) {
        	entry.setSdMulticastAddress(addr);
        } else {
        	AutorsarCommonUtils.logEx("in setmulticastEntryAddr vlanId is NULL: " +vlanId);
        }
    }
    
    /**
     * 在指定条目中 修改 MulticastEntry 的protocol （通过vlan id）
     */
    public void setMulticastEntryProtocol(String vlanId, String proto) {
    	AutorsarCommonUtils.logEx("in setmulticastEntryProtocol vlanId: " + vlanId);
    	AutorsarCommonUtils.logEx("in setmulticastEntryProtocol proto: " + proto);
    	MulticastEntry entry = getMulticastEntryByVlan(vlanId);
        if (entry != null) {
        	entry.setProtocol(proto);
        } else {
        	AutorsarCommonUtils.logEx("in setmulticastEntryProtocol vlanId is NULL: " +vlanId);
        }
    }
}
